package com.jellyfishumbrella;

import java.util.Random;

import com.jellyfishumbrella.GL.Keyframe;
import com.jellyfishumbrella.GL.MeshGL;
import com.jellyfishumbrella.GL.ThreadLoop;
import com.jellyfishumbrella.GL.Vector3d;
import com.jellyfishumbrella.GL.Primitives.Cube;

public class Test {
	private static Random		R	= new Random();
	
	public static void makeCubes(GameLogic logic) {
		TestCubeThread testCubeThread = new TestCubeThread(logic);
		testCubeThread.start();
	}

	public static class TestCubeThread extends ThreadLoop{
		GameLogic logic;
		int maxCubes = 100;
		int numCubes = 0;
		Cube cube;
		
		public TestCubeThread(GameLogic logic){
			this.logic = logic;
			cube = new Cube(1.0);
			logic.allThreads.add(this);
		}
		
		public void run(){
			logic.renderGL.add(cube);
			while (!done) {
				testCube(cube.clone(), R.nextDouble() * 1000 - 500, R.nextDouble() * 1000 - 500, R.nextDouble() * 1000 - 500, R.nextInt(10) + 3, R.nextDouble() * 3 + 3, logic);
				numCubes++;
				System.out.println(numCubes+"   "+maxCubes);
				try {
					sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(numCubes >= maxCubes){
					done = true;
				}
			}
			logic.renderGL.remove(cube);
		}
		
		public static void testCube(MeshGL cube, double X, double Y, double Z, double scale, double timeScale, GameLogic logic) {
			Keyframe key0 = new Keyframe((int) Math.floor(0 * timeScale));
			Keyframe key2000 = new Keyframe((int) Math.floor(200 * timeScale));
			Keyframe key4000 = new Keyframe((int) Math.floor(400 * timeScale));

			key0.pos = new Vector3d(R.nextInt(100) + X - 50, 100 + Y, R.nextInt(100) + Z - 50);
			key0.scale = new Vector3d(scale,scale,scale);
			key2000.pos = new Vector3d(X, 0 + Y, Z);
			key2000.rot = new Vector3d(R.nextInt(360),R.nextInt(360),R.nextInt(360));
			key2000.scale = new Vector3d(scale,scale,scale);
			key4000.pos = key0.pos.clone();
			key4000.rot = new Vector3d(360,360,360);
			key4000.scale = new Vector3d(scale,scale,scale);

			cube.keyframes.clear();
			cube.keyframes.add(key0);
			cube.keyframes.add(key2000);
			cube.keyframes.add(key4000);

			cube.goToKeyframe(key0);
			logic.animator.add(cube);
			logic.renderGL.add(cube);
			//logic.octree.add(cube);
		}
	}
}
